package com.blackfish.module;

import com.jfinal.core.Controller;

import com.jfinal.plugin.activerecord.Record;
import org.json.JSONObject;
import org.json.JSONArray;

import java.util.List;

import com.blackfish.bag.BagService;
import com.blackfish.common.model.Module;
import com.blackfish.common.model.Style;
import com.blackfish.module.ModuleService;

import com.jfinal.aop.Before;

@Before(ModuleInterceptor.class)
public class ModuleController extends Controller {
    static ModuleService service = new ModuleService();

    public void index() {
        int bag_id = getAttr("bag_id");
        Object value = service.paginate(1, 10, bag_id);
        setAttr("bagPage", value);
        render("index.html");
    }

    public void sendEvent() {
        render("event.html");
    }

    public void table() {
        List<Record> modules= service.findAllView();
        renderJson(modules);
    }

    public void getCustomer() {
        List<String> list = service.getCustomerList();
        setAttr("customers", list);
        renderNull();
    }

    public void getStyle() {
        List<String> list = service.getStyleList();
        setAttr("styles", list);
        renderNull();
    }

    public void delete() {
        String[] idStrings = this.getParaValues("ids[]");
        for (String idString : idStrings) {
            int id = Integer.valueOf(idString).intValue();
            if (service.deleteById(id) == false) {
                renderText("error");
                return;
            }
        }
        renderText("ok");
    }

    public void add() {
        String bag = getPara("bag");
        String path = getPara("path");
        String type = getPara("type");
        String iom = getPara("iom");
        String customer = getPara("customer");
        String style = getPara("style");
        String title = getPara("title");
        String[] keyList = getParaValues("tabkey[]");
        String[] valList = getParaValues("tabval[]");
        String[] actList = getParaValues("actkey[]");
        int modId = 0;
        boolean request = false;
        if (iom.isEmpty()) {
            modId = service.saveModule(bag, path, type, customer, style, title);
        } else {
            modId = service.saveModule(bag, path, type, iom, customer, style, title);
        }
        if (modId != 0) {
            for (int i = 0; i < keyList.length; i++) {
                request = service.saveComAndAct(modId, keyList[i], valList[i], actList[i]);
            }
            if (request) {
                renderText("ok");
            } else {
                renderText("error");
            }
        } else {
            renderText("error");
        }
    }

    public void edit() {
        String iom = getPara("iom");
        String customer = getPara("customer");
        String title = getPara("title");
        String idString = getPara("id");
        String path = getPara("path");
        int id = Integer.valueOf(idString).intValue();
        if (service.editModule(id, title, customer, iom, path)) {
            render("ok");
        } else {
            render("error");
        }
    }
}
